﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.Framework.Common.Enums;
using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.Framework.Client;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.CharmManager.Client.App;

namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public partial class frmCallInfo : FormBase
    {
        private DBEntitySchedule callInfo = null;

        public frmCallInfo(DBEntitySchedule call)
        {
            InitializeComponent();

            cbxPlanTimeState.SelectedIndex = 0;
            cbxState.SelectedIndex = 0;
            cbxScheduleType.SelectedIndex = 0;
            datePlanStartTime.Value = DateTime.Now;
            datePlanEndTime.Value = DateTime.Now;
            dateScheduleTime.Value = DateTime.Now;
            dateScheduleTimeHour.Value = DateTime.Now;
            cbxScheduleType.SelectedIndex = 0; 

            if (call != null)
            {
                this.callInfo = (DBEntitySchedule)call.Clone();
                initControl(callInfo);
            }
        }

        /// <summary>
        /// 编辑模式下的初始化
        /// </summary>
        /// <param name="scheduleInfo"></param>
        /// <param name="schduelInfoCustom"></param>
        public void initControl(DBEntitySchedule scheduleInfo)
        {
            txtScheduleName.Text = scheduleInfo.SCHEDULENAME;
            datePlanStartTime.Value = scheduleInfo.SPLANSTARTTIME;
            datePlanEndTime.Value = scheduleInfo.SPLANENDTIME;
            dateScheduleTime.Value = scheduleInfo.SCHEDULETIME;
            dateScheduleTimeHour.Value = scheduleInfo.SCHEDULETIME;
            txtEmployeeName.Text = scheduleInfo.SPLANEMPLOYEE;
            memSPlanMemo.Text = scheduleInfo.SPLANMEMO;

            string chineseStr = DBEntitySchedule.ENUM_STATE.EnumStrToChineseStr(scheduleInfo.STATE);
            int idx = cbxState.Items.IndexOf(chineseStr);
            if (idx != -1)
            {
                cbxState.SelectedIndex = idx;
            }

            chineseStr = DBEntitySchedule.ENUM_SPLANTIMESTATE.EnumStrToChineseStr(scheduleInfo.SPLANTIMESTATE);
            idx = cbxPlanTimeState.Items.IndexOf(chineseStr);
            if (idx != -1)
            {
                cbxPlanTimeState.SelectedIndex = idx;
            }

            chineseStr = DBEntitySchedule.ENUM_SCHEDULETYPE.EnumStrToChineseStr(scheduleInfo.SCHEDULETYPE);
            idx = cbxScheduleType.Items.IndexOf(chineseStr);
            if (idx != -1)
            {
                cbxScheduleType.SelectedIndex = idx;
            }

        }

        /// <summary>
        /// 获取界面设置信息
        /// </summary>
        /// <param name="scheduleInfo">提醒信息类</param>
        /// <param name="schduleCustom">预约会员信息类</param>
        /// <param name="error">返回错误信息</param>
        /// <returns></returns>
        public bool getInputScheduleInfo(ref DBEntitySchedule scheduleInfo, out string error)
        {
            error = string.Empty;
            scheduleInfo.STATE = DBEntitySchedule.ENUM_STATE.ChineseStrToEnumStr(cbxState.SelectedItem.ToString());

            if (txtScheduleName.Text.Trim().Equals(string.Empty))
            {
                error = "请输入提醒计划名称。";
                return false;
            }
            if (datePlanStartTime.Value.CompareTo(datePlanEndTime.Value) > 0)
            {
                error = "提醒结束时间不能够小于开始时间。";
                return false;
            }
            if (datePlanStartTime.Value.CompareTo(dateScheduleTime.Value) == -1 || datePlanEndTime.Value.CompareTo(dateScheduleTime.Value) == 1)
            {
                error = "下次提醒时间不能小于提醒开始时间，或者大于提醒结束时间。";
                return false;
            }


            if (txtEmployeeName.Text.Trim().Equals(string.Empty))
            {
                error = "请输入员工姓名。";
                return false;
            }
            if (scheduleInfo == null)
                scheduleInfo = new DBEntitySchedule();

            scheduleInfo.SALONID = 1;//默认当前一个店
            scheduleInfo.SCHEDULENAME = txtScheduleName.Text.Trim();
            scheduleInfo.SPLANSTARTTIME = datePlanStartTime.Value;
            scheduleInfo.SPLANENDTIME = datePlanEndTime.Value;
            scheduleInfo.STATE = DBEntitySchedule.ENUM_STATE.ChineseStrToEnumStr(cbxState.SelectedItem.ToString());
            scheduleInfo.SCHEDULETIME = new DateTime(dateScheduleTime.Value.Year, dateScheduleTime.Value.Month, dateScheduleTime.Value.Day,
                dateScheduleTimeHour.Value.Hour, dateScheduleTimeHour.Value.Minute, dateScheduleTimeHour.Value.Second);

            scheduleInfo.SPLANTIMESTATE = DBEntitySchedule.ENUM_SPLANTIMESTATE.ChineseStrToEnumStr(cbxPlanTimeState.SelectedItem.ToString());
            scheduleInfo.SCHEDULETYPE = DBEntitySchedule.ENUM_SCHEDULETYPE.ChineseStrToEnumStr(cbxScheduleType.SelectedItem.ToString());
            scheduleInfo.SPLANEMPLOYEE = txtEmployeeName.Text.Trim();
            scheduleInfo.SPLANMEMO = memSPlanMemo.Text.Trim();
            scheduleInfo.SYSTIME = DateTime.Now;

            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            DBEntitySchedule tmpScheduleInfo = new DBEntitySchedule();
            string error = string.Empty;
            if (!getInputScheduleInfo(ref tmpScheduleInfo, out error))
            {
                MsgBox.MsgInfo(error);
                return;
            }
            bool bState = false;
            if(callInfo == null){
                callInfo = (DBEntitySchedule)tmpScheduleInfo.Clone();
                bState = BussinessProxyManager.Instance.InsertScheduleCustom(callInfo, null, out error);

            }else{
                callInfo = (DBEntitySchedule)tmpScheduleInfo.Clone();
                bState = BussinessProxyManager.Instance.UpdateScheduleCustom(callInfo, null, null, out error);
            }
            if (!bState)
            {
                MsgBox.MsgInfo("保存数据出现错误：" + error);
                return;
            }
            this.DialogResult = DialogResult.OK;           
            
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (!MsgBox.MsgQue("是否要退出？"))
            {
                return;
            }
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
